<?php
class SiteSaveListAction extends AdminAction
{
	public function __construct()
	{
		parent::__construct();
		$this->responseType = 'json';
	}
	
	protected function afterConstruct()
	{
		$this->parseDtds('site','list');
	}
	
	public function execute()
	{
		$data			= $this->getValidData();
		
		//推广数量
		if($data['start_commissionNum']!='' || $data['end_commissionNum']!='')
		{
			if(!($data['start_commissionNum']!='' && $data['end_commissionNum']!=''))
			{
				$this->data['error_messages']['end_commissionNum']	= array('both'=>'两个只能是都填或者都不填');
			}
			else
			{
				if($data['start_commissionNum']>=$data['end_commissionNum'])
				{
					$this->data['error_messages']['end_commissionNum']	= array('lower'=>'起始价格必须小于最高价格');
				}
			}
		}
		//佣金比率
		if($data['start_commissionRate']!='' || $data['end_commissionRate']!='')
		{
			if(!($data['start_commissionRate']!='' && $data['end_commissionRate']!=''))
			{
				$this->data['error_messages']['end_commissionRate']	= array('both'=>'两个只能是都填或者都不填');
			}
			else
			{
				if($data['start_commissionRate']>=$data['end_commissionRate'])
				{
					$this->data['error_messages']['end_commissionRate']	= array('lower'=>'起始佣金比率必须小于最高佣金比率');
				}
			}
		}
		
		//卖家信用
		$level	= array('1heart'=>1,'2heart'=>2,'3heart'=>3,'4heart'=>4,'5heart'=>5,'1diamond'=>6,'2diamond'=>7,'3diamond'=>8,'4diamond'=>9,'5diamond'=>10,'1crown'=>11,'2crown'=>12,'3crown'=>13,'4crown'=>14,'5crown'=>15,'1goldencrown'=>16,'2goldencrown'=>17,'3goldencrown'=>18,'4goldencrown'=>19,'5goldencrown'=>20,);
		if(!(array_key_exists($data['start_credit'],$level) && array_key_exists($data['end_credit'],$level)))
		{
			$this->data['error_messages']['end_credit']	= array('both'=>'无效的起始信用和最高信用');
		}
		else
		{
			if($level[$data['start_credit']]>=$level[$data['end_credit']])
			{
				$this->data['error_messages']['end_credit']	= array('lower'=>'起始信用必须小于最高信用');
			}
		}
		if(!empty($this->data['error_messages']))
		{
			$this->code	= 407;
		}
		else
		{
			if($data['guarantee']!='true')
			{
				$data['guarantee']	= 'false';
			}
			$settingService	= new SettingService();
			$skey			= 'list_setting';
			$tmp			= $settingService->get($skey);
			if(!$tmp)
			{
				$settingService->add($skey,$data,true);
			}
			else
			{
				$settingService->update($skey,$data,false,true);
			}
			$this->code	= 200;
		}
	}
}
